use color_eyre::{eyre::WrapErr, owo_colors::OwoColorize, Result};

use edit::{edit_file, Builder};
use std::{
  fs,
  io::{Read, Seek, SeekFrom, Write},
  path::PathBuf,
};

// pub fn write(garden_path: PathBuf, title: Option<String>) -> Result<()> {
//   dbg!(&garden_path, &title);

//   // EDITOR="code -w" cargo run -- write
//   let template = "# ";
//   let content_from_user =
//     edit::edit(template).wrap_err("Unable to read writing")?;

//   dbg!(content_from_user);
//   todo!()
// }

pub fn write(garden_path: PathBuf, title: Option<String>) -> Result<()> {
  dbg!(&garden_path, &title);
  // 创建目录
  fs::create_dir_all(&garden_path)?;

  // EDITOR="code -w" cargo run -- write
  let (mut file, filepath) = Builder::new()
    .suffix(".md")
    .rand_bytes(5)
    .tempfile_in(&garden_path)
    .wrap_err("Failed to create wip file")?
    .keep()
    .wrap_err("Failed to keep tempfile")?;

  file.write_all(b"# ")?;

  edit_file(&filepath)?;

  let mut contents = String::new();
  file.seek(SeekFrom::Start(0))?;
  file.read_to_string(&mut contents)?;

  dbg!(&contents);

  let document_title = title.or_else(|| {
    contents
      .lines()
      .find(|line| line.starts_with("# "))
      .map(|maybe_line| maybe_line.trim_start_matches("# ").to_string())
  });

  let filename = match document_title {
    Some(raw_title) => confirm_filename(&raw_title),
    None => ask_for_filename(),
  }?;
  dbg!(&filename);

  let mut i = 0;

  loop {
    let dest_filename = format!(
      "{}{}",
      filename,
      if i == 0 {
        "".to_string()
      } else {
        i.to_string()
      }
    );
    let mut dest = garden_path.join(dest_filename);
    dest.set_extension("md");
    if dest.exists() {
      i += 1;
    } else {
      fs::rename(filepath, &dest)?;
      break;
    }
  }
  Ok(())
}

fn ask_for_filename() -> Result<String> {
  rprompt::prompt_reply(&format!(
    "{}",
    "\
Enter filename
> "
      .blue()
      .bold(),
  ))
  .wrap_err("Failed to get filename")
  .map(|title| slug::slugify(title))
}

fn confirm_filename(raw_title: &str) -> Result<String> {
  loop {
    let result = rprompt::prompt_reply(&format!(
      "\
{} {}
Do you want a different title? [y/N]: ",
      "Current title:".green().bold(),
      raw_title
    ))
    .wrap_err("Failed to get input for y/n question")?;

    match result.as_str() {
      "y" | "Y" => break ask_for_filename(),
      "n" | "N" | "" => {
        break Ok(slug::slugify(raw_title));
      }
      _ => {}
    }
  }
}
